﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    /// <summary>
    /// 开服竞晒
    /// </summary>
    public class UIActivityOpenServerRace : UIActivityTemplate
    {
        public UILabel labActivityTip;
        public UILabel labCountDown;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject activityItemPref;

        System.DateTime endTime;
        bool startCountDown = false;
        float deltaTime = 0;

        protected override void PrepareData()
        {
            activityType = ActivityType.OpenServerRace;
        }

        public override void Show()
        {
            base.Show();
            
            CSActivityInfo msg = new CSActivityInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
        }

        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;
                
                activityId = msg.activityId;

                //截止时间
                endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                labActivityTip.text = string.Format(TableTextBlo.GetText("open_sever_activity_tip"), endTimeStr, msg.param);

                //倒计时
                startCountDown = true;

                //奖励内容
                foreach (var item in simpleGrid.childs)
                {
                    Destroy(item);
                }
                simpleGrid.childs.Clear();

                for (int i = 0; i < msg.itemList.Count; i++)
                {
                    GameObject obj = Instantiate(activityItemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    obj.GetComponent<AcOpenServerRaceItem>().SetActivityItem(msg.itemList[i],i);
                }
            }
        }

        void Update()
        {
            if(startCountDown)
            {
                deltaTime += Time.deltaTime;
                if(deltaTime >= 1)
                {
                    deltaTime = 0;

                    System.DateTime curtime = ServerTimerTool.Java2CSTimeLocale(ServerTimerTool.CurrentUTCJavaTime());
                    System.TimeSpan timespan1 = endTime.Subtract(curtime);
                    if (timespan1.TotalSeconds > 0)
                    {
                        string days = "";
                        if (timespan1.Days > 0)
                            days = timespan1.Days.ToString() + TableTextBlo.GetText("day");
                        labCountDown.text = days + string.Format("{0:d2}", timespan1.Hours) + ":" + string.Format("{0:d2}", timespan1.Minutes) + ":" + string.Format("{0:d2}", timespan1.Seconds);
                    }
                    else
                    {
                        startCountDown = false;
                    }
                }
            }
        }
    }
}